﻿using AutoMapper;
using B.S.ProcessEgine.API.Applocations.Commands.FormNodes;
using B.S.ProcessEgine.Domain.Entities;
using B.S.ProcessEgine.ErrorCode;
using B.S.ProcessEgine.Infractructure.Interfaces;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Transactions;

namespace B.S.ProcessEgine.API.Applocations.CommandHandlers.FormNodes
{
    public class UpdFormNodeCommandHandler : IRequestHandler<UpdFormNodeCommand, ApiResult>
    {
        private readonly IFormNodeRepository formNode;
        private readonly IMapper mapper;
        private readonly IFormInfoRepository formInfo;
        private readonly IFormTypeRepository formType;

        public UpdFormNodeCommandHandler(IFormNodeRepository formNode,IMapper mapper,IFormInfoRepository formInfo,IFormTypeRepository formType)
        {
            this.formNode = formNode;
            this.mapper = mapper;
            this.formInfo = formInfo;
            this.formType = formType;
        }
        public async Task<ApiResult> Handle(UpdFormNodeCommand request, CancellationToken cancellationToken)
        {
            try
            {
                //查询表单节点名称是否存在
                var list = await formNode.GetAsync(x => x.NodeId == request.NodeId);
                if (list.NodeName == request.NodeName)
                {
                    return ApiResult.Fail("表单节点名称已存在", StateCodeEnum.Fail);
                }
                list.NodeName = request.NodeName;


                await formNode.UpdateAsync(list);
                return ApiResult.Success(StateCodeEnum.Ok);
             
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}
